package juc.threadLocal;

import java.util.concurrent.TimeUnit;

public class ThreadLocalDemo2 {
    ThreadLocal<String> threadLocal =new ThreadLocal<>();

    public String getName() {
        return threadLocal.get();
    }

    public void setName(String name) {
        threadLocal.set(name);
    }

    /*对ThreadLocalDemo优化
     * ThreadLocal 线程私有容器
     * */
    public static void main(String[] args) throws InterruptedException {
        ThreadLocalDemo2 demo = new ThreadLocalDemo2();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                String str = Thread.currentThread().getName();
                demo.setName(str);
                System.out.println(str + demo.getName());
            }, "" + i).start();
        }
        TimeUnit.SECONDS.sleep(1);
    }
}